package com.wangijun.yuan.web.api;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.wangijun.yuan.web.base.CommonPage;
import com.wangijun.yuan.web.base.CommonResult;
import com.wangijun.yuan.core.base.YesNoEnum;
import com.wangijun.yuan.core.domain.Article;
import com.wangijun.yuan.web.model.dto.CreationQryArtParam;
import com.wangijun.yuan.web.model.dto.CreationQryDraftParam;
import com.wangijun.yuan.web.model.vo.CreationArticleVO;
import com.wangijun.yuan.web.service.ArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "8-个人创作中心接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/creation")
public class CreationController {
    private final ArticleService articleService;

    @Operation(summary = "文章列表", description = "查询文章列表")
    @PostMapping("/article")
    CommonResult<CommonPage<CreationArticleVO>> articleList(@RequestBody CreationQryArtParam param) {
        return CommonResult.data(articleService.creationQryArticle(param));
    }

    @Operation(summary = "草稿文章列表", description = "查询文章列表")
    @PostMapping("/draft")
    CommonResult<CommonPage<CreationArticleVO>> draftList(@RequestBody CreationQryDraftParam param) {
        return CommonResult.data(articleService.draftList(param));
    }


    public record ChartDataVO(
            @Schema(description = "X轴")
            List<String> dataX,
            @Schema(description = "Y轴")
            List<Integer> dataY
    ) {
    }

    @Operation(summary = "Bar图表数据", description = "前端图表数据")
    @GetMapping("/chart-data-bar")
    CommonResult<ChartDataVO> chartDataBar() {
        List<Article> list =
                Db.lambdaQuery(Article.class)
                        .eq(Article::getCreateBy, StpUtil.getLoginIdAsLong())
                        .eq(Article::getStatus, YesNoEnum.YES.getCode())
                        .orderByDesc(Article::getEditTime)
                        .last("limit 7").list();
        return CommonResult.data(new ChartDataVO(
                list.stream().map(Article::getTitle).toList(),
                list.stream().map(Article::getReadNum).toList()
        ));
    }
}
